package com.baizan.daka.ui.presenter

import com.baizan.daka.base.BasePresenter
import com.baizan.daka.mvp.model.OrderModel
import com.baizan.daka.mvp.model.bean.PayChannelBean
import com.baizan.daka.rx.Subscriber.CommonSubscriber
import com.baizan.daka.showToast
import com.baizan.daka.ui.contract.PayContract
import io.reactivex.Observer
import io.reactivex.disposables.Disposable

class PayPresenter(val mView: PayContract.View) : BasePresenter(),
    PayContract.Presenter {


    private val orderModel: OrderModel by lazy {
        OrderModel()
    }

    override fun pay(goodsId: Long, goodsType: Int) {
        mView.showLoadingDialog()
        orderModel.payOrder(goodsId, goodsType)
            .subscribe(object : CommonSubscriber<String>(compositeDisposable) {
                override fun onCallNext(t: String?) {
                    mView.paySuccess(t!!)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }
            })
    }

    override fun payHtml(goodsId: Long, goodsType: Int, payType: String) {
        mView.showLoadingDialog()
        orderModel.payOrderHtml(goodsId, goodsType, payType)
            .subscribe(object : Observer<String> {
                override fun onComplete() {
                    mView.hideLoadingDialog()
                }

                override fun onSubscribe(d: Disposable) {
                    mView.hideLoadingDialog()
                }

                override fun onNext(t: String) {
                    mView.payHtmlSuccess(t)
                    mView.hideLoadingDialog()
                }

                override fun onError(e: Throwable) {
                    e.message?.let {
                        showToast(it)
                    }
                    mView.hideLoadingDialog()
                }
            })

    }

    override fun getPayChannelList() {
        mView.showLoadingDialog()
        orderModel.getPayChannelList()
            .subscribe(object : CommonSubscriber<List<PayChannelBean>>(compositeDisposable) {
                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }
                override fun onCallNext(t: List<PayChannelBean>?) {
                    mView.getPayChannelListSuccess(t!!)

                }


            })
    }
}
